package com.toobigtofail
{
	 import net.flashpunk.Entity;
	 import net.flashpunk.FP;
	 import net.flashpunk.graphics.Graphiclist;
	 import net.flashpunk.graphics.Image;
	 import net.flashpunk.graphics.Text;
	 import net.flashpunk.utils.Input;
	 import net.flashpunk.utils.Key;
	 
	 public class InputDisplay extends Entity
	 {
		  [Embed(source="/assets/gfx/cursor.png")] private static const CURSOR_PNG:Class;
		  
		  // Constants
		  private static const BACKSPACE_DELAY:Number = 0.20;
		  private static const TEXT_X:int = 3;
		  private static const CURSOR_X:int = 2;
		  private static const CURSOR_BLINK_TIME:Number = 0.5;
		  
		  private var shouldUpdateGraphic:Boolean = true;
		  private var backspaceDelay:Number = BACKSPACE_DELAY;
		  private var allowBackspace:Boolean = true;
		  private var cursorBlinkTimer:Number = CURSOR_BLINK_TIME;
		  
		  private var inputText:String = "";
		  private var cursor:Image = new Image(CURSOR_PNG);
		  private var graphicList:Graphiclist = new Graphiclist;
		  private var _text:Text;
		  
		  public function InputDisplay(x:Number=0, y:Number=0)
		  {
			   this.x = x;
			   this.y = y;
			   
			   cursor.x = CURSOR_X;
			   cursor.y = 2;
			   
			   graphicList.add(cursor);
			   _text = new Text("", TEXT_X);
			   graphicList.add(_text);
			   
			   graphic = graphicList;
		  }
		  
		  override public function update():void {
			   
			   cursorBlinkTimer -= FP.elapsed;
			   if (cursorBlinkTimer <= 0) {
				    cursor.visible = !cursor.visible;
				    cursorBlinkTimer = CURSOR_BLINK_TIME;
			   }
			   
			   if (!allowBackspace) {
				    backspaceDelay -= FP.elapsed;
				    if (backspaceDelay <= 0) {
					     allowBackspace = true;
				    }
			   }
			   
			   super.update();
		  }
		  
		  public function keypress(key:int):int {
			   
			   if (key == Key.BACKSPACE && allowBackspace) {
				    inputText = inputText.substr(0,inputText.length-1);
				    updateText();
				    allowBackspace = false;
				    backspaceDelay = BACKSPACE_DELAY;
				    
				    return key;
			   }
			   else if (key == Key.ENTER) {
				    //inputText = "";
				    //updateText();
				    
				    return key;
			   }
			   else if (key == Key.SPACE) {
				    inputText += " ";
				    updateText();
				    
				    return key;
			   }
			   else if ((key >= Key.A && key <= Key.Z) ||
				      (key >= Key.DIGIT_0 && key <= Key.DIGIT_9)) {
				    inputText += Key.name(key);
				    
				    updateText();
				    
				    return key;
			   }
			   
			   return -1;
		  }
		  
		  private function updateText():void {
			   graphicList.removeAt(graphicList.count-1);
			   _text = new Text(inputText.substring(inputText.length-15), TEXT_X);
			   graphicList.add(_text);
			   
			   cursor.x = CURSOR_X + _text.width;
		  }
		  
		  public function length():int {
			   return inputText.length;
		  }
		  
		  public function get text():String {
			  return inputText;
		  }
		  
		  public function clear():void {
			   inputText = "";
			   cursor.x = CURSOR_X;
			   graphicList.removeAt(graphicList.count-1);
			   graphicList.add(new Text(inputText.substring(inputText.length-15), TEXT_X));
		  }
	 }
}